home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / shell / ncd-0.000 / ncd-0 / ncd-0.9.8 / curs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-06  |  6.3 KB  |  333 lines

  1. #include <stdio.h>
  2. #include <ncurses.h>
  3.  
  4. #include "ncd.h"
  5.  
  6. /*************************************************************************/
  7.  
  8. void ioInitialize(void)
  9. {
  10.     if (!(initscr())) {
  11.         cleanUp();
  12.         fprintf(stderr, "error: can't initialize screen\n");
  13.         exit(1);
  14.     }
  15.  
  16.     _cursesOn = 1;
  17.     
  18.     _lines = LINES;
  19.     _cols = COLS;
  20.     if (_lines < MIN_LINES) {
  21.         cleanUp();
  22.         fprintf(stderr, "error: not enought screen lines\n");
  23.         exit(1);
  24.     }
  25.     if (_cols < MIN_COLS) {
  26.         cleanUp();
  27.         fprintf(stderr, "error: not enought screen columns\n");
  28.         exit(1);
  29.     }
  30.  
  31.     nonl();
  32. /* intrflush(stdscr,FALSE); */
  33.  
  34.     cbreak();
  35.     noecho();
  36. /*     halfdelay(10); */
  37.     keypad(stdscr, TRUE);
  38.     meta(stdscr, TRUE); 
  39. /*    notimeout(stdscr, TRUE); */
  40.  
  41.     prepareColorModes();
  42.     attrset(getColorMode(0));
  43.     clear();
  44. }
  45.  
  46. /*************************************************************************/
  47.  
  48. void ioTerminate(void)
  49. {
  50.     nl();
  51.     nocbreak();
  52.     echo();
  53.  
  54.     standend();
  55.     clear();
  56.     endwin();
  57.     refresh();
  58.     
  59.     _cursesOn = 0;
  60. }
  61.  
  62. /*************************************************************************/
  63.  
  64. void prepareColorModes(void)
  65. {
  66.     start_color();
  67.     _use_color = has_colors();
  68.     if (_use_color) {
  69.         init_pair(1, COLOR_WHITE, COLOR_BLUE);
  70.         init_pair(2, COLOR_BLACK, COLOR_CYAN);
  71.     }
  72.  
  73.     if (_lineart == 2)
  74.         _lineart = _use_color;
  75.  
  76. }
  77.  
  78. /*************************************************************************/
  79. /* 0 = normal/normalcolor, 1 = reverse/reversecolor */
  80.  
  81. int getColorMode(int c)
  82. {
  83.     switch (c) {
  84.     case 1:
  85.         if (_use_color)
  86.             return COLOR_PAIR(2);
  87.         else
  88.             return A_REVERSE;
  89.     case 0:
  90.     default:
  91.         if (_use_color)
  92.             return COLOR_PAIR(1);
  93.         else
  94.             return A_NORMAL;
  95.     }
  96. }
  97.  
  98. /*************************************************************************/
  99. /* getKey / kbHit variables */
  100.  
  101. int __gotKey;   /* != 0 if kbHit got a key.  I use this instead 
  102.                   of calling ungetch(), because that does not works well */
  103. int __theKey;   /* the key that kbHit() has got */
  104.  
  105. /*************************************************************************/
  106. /* delay=0 -> no delay. <0 -> block */
  107.  
  108.  
  109. int getKey(int delay)
  110. {
  111.     if (__gotKey) {
  112.         __gotKey = 0;
  113.         return __theKey;
  114.     }
  115.     
  116.     timeout(delay); 
  117.     return getch();
  118. }
  119.  
  120. /*************************************************************************/
  121.  
  122. int kbHit(void)
  123. {
  124.     int ch;
  125.  
  126.     if (__gotKey)
  127.         return 1;
  128.         
  129.     ch = getKey(0);
  130.     if (ch != ERR) {
  131.         __gotKey = 1;
  132.         __theKey = ch;
  133.         
  134.         return 1;
  135.     }
  136.  
  137.     return 0;
  138. }
  139.  
  140. /*************************************************************************/
  141.  
  142. void showHelp()
  143. {
  144.     if ((_lines<HLP_LINES)||(_cols<HLP_COLS))
  145.         showHelpNoSpace();
  146.     else
  147.         showHelpScreen(); 
  148.         
  149.     getKey(-1);
  150. }
  151.  
  152. /*************************************************************************/
  153. /* quit = 2 -> accept
  154. quit = 1 -> cancel
  155. quit = 3 -> rescan 
  156. quit = 4 -> toggle scope */
  157.  
  158.  
  159. DirNode * selectANode( int * quit )
  160. {
  161. #define CTRL(n)  (n-'a'+1)
  162.  
  163.     int ch;
  164.  
  165.     *quit = 0;
  166.  
  167.     if (!_cursesOn)
  168.         ioInitialize();
  169.  
  170.     _curNode = locatePathOrSo(_cwd,NULL);
  171.     _x0 = 0;
  172.     if (_lastNode->y < (_lines-5))
  173.         _y0 = 0;
  174.     else {
  175.         _y0 = _curNode->y - ((_lines - 6) / 2);
  176.         if (_lastNode->y-_y0<_lines-6)
  177.             _y0 = _lastNode->y-(_lines-6);
  178.     }
  179.     _searchPath[0] = 0;
  180.     
  181.     paintLayout();
  182.     dataRefresh(_x0, _y0, _curNode);
  183.  
  184.     do {
  185.         ch = getKey(-1);
  186.  
  187.         switch (ch) {
  188.  
  189.         case KEY_DOWN:
  190.         case CTRL('n'):
  191.             _searchPath[0] = 0;
  192.             dataRefresh(_x0, _y0, getNodeCursDown(_curNode));
  193.             break;
  194.         case KEY_UP:
  195.         case CTRL('p'):
  196.             _searchPath[0] = 0; 
  197.             dataRefresh(_x0, _y0, getNodeCursUp(_curNode));
  198.             break;
  199.         case KEY_LEFT:
  200.         case CTRL('b'):
  201.             _searchPath[0] = 0; 
  202.             dataRefresh(_x0, _y0, getNodeCursLeft(_curNode));
  203.             break;
  204.         case KEY_RIGHT:
  205.         case CTRL('f'):
  206.             _searchPath[0] = 0; 
  207.             dataRefresh(_x0, _y0, getNodeCursRight(_curNode));
  208.             break;
  209.         case KEY_HOME:
  210.         case CTRL('a'):
  211.             _searchPath[0] = 0;
  212.             dataRefresh(_x0, _y0, _rootNode);
  213.             break;
  214.         case KEY_END:
  215.         case CTRL('e'):
  216.             _searchPath[0] = 0;
  217.             dataRefresh(_x0, _y0, getLastDescendant(_curNode));
  218.             break;
  219.         case KEY_PPAGE:
  220.         case CTRL('u'):
  221.             _searchPath[0] = 0;
  222.             if (_curNode->y < (_lines - 6))
  223.                 dataRefresh(0, 0, _rootNode );
  224.             else
  225.                 dataRefresh(_x0, _y0 - (_lines - 6), getFirstNodeInLevel(_rootNode, _curNode->y - (_lines - 6)));
  226.             break;
  227.         case KEY_NPAGE:
  228.         case CTRL('v'):
  229.             _searchPath[0] = 0;
  230.             if (_curNode->y + (_lines - 6) >=_lastNode->y)
  231.                 dataRefresh(_x0, _y0, _lastNode );
  232.             else {
  233.                 if (_y0+(_lines-6)*2<_lastNode->y)
  234.                     dataRefresh(_x0, _y0 + (_lines - 6), getFirstNodeInLevel(_rootNode, _curNode->y + (_lines - 6)));
  235.                 else
  236.                     dataRefresh(_x0, _lastNode->y-(_lines-6), getFirstNodeInLevel(_rootNode, _curNode->y + (_lines - 6)));        
  237.             }
  238.             break;
  239.         case CTRL('o'):
  240.         case KEY_F(7):
  241.             _searchPath[0] = 0;
  242.             dataRefresh(_x0, _y0, getNodeLnUp(_curNode));
  243.             break;
  244.         case CTRL('j'):
  245.         case KEY_F(8):
  246.             _searchPath[0] = 0;
  247.             dataRefresh(_x0, _y0, getNodeLnDn(_curNode));
  248.             break;            
  249.         case CTRL('t'):
  250.             _searchPath[0] = 0; 
  251.             dataRefresh(_x0, _y0, getNodePrev(_curNode));
  252.             break;             
  253.         case CTRL('x'):
  254.         case KEY_F(10):
  255.             *quit = 1;
  256.             break;
  257.         case CTRL('k'):
  258.         case KEY_F(9):
  259.             editSearch(0);
  260.             break;
  261.         case KEY_BACKSPACE:
  262.             editSearch(8);
  263.             break;                
  264.         case CTRL('i'):
  265.         case KEY_F(1):
  266.             showHelp();
  267.             paintLayout();
  268.             dataRefresh(_x0, _y0, _curNode);
  269.             break;
  270.         case KEY_F(2):
  271.         case CTRL('r'): 
  272.             *quit = 3;
  273.             break;
  274.         case KEY_F(3):
  275.         case CTRL('y'):
  276.             *quit = 4;
  277.             strcpy(_cwd,getNodeFullPath(_curNode, 0, 0, NULL, 1 ));
  278.             break;
  279.         case CTRL('g'):
  280.         case KEY_F(6):
  281.             _showlink = !_showlink;
  282.             paintLayout();
  283.             dataRefresh(_x0, _y0, _curNode);
  284.             break;
  285.         case CTRL('l'):            
  286.         case KEY_F(5):
  287.             _searchPath[0] = 0;
  288.             dataRefresh(_x0, _y0, getNodeFollowLink(_rootNode, _curNode));
  289.             break;
  290.         case KEY_DL:
  291.         case KEY_EOL:
  292.         case CTRL('d'):
  293.         case KEY_DC:
  294.             _searchPath[0] = 0; 
  295.             dataRefresh(_x0, _y0, _curNode);
  296.             break;
  297.         case CTRL('w'):
  298.         case KEY_F(4):
  299.             clear();
  300.             paintLayout();
  301.             dataRefresh(_x0, _y0, _curNode);
  302.             break;
  303.         case KEY_ENTER:
  304.         case CTRL('m'):
  305.             *quit = 2;
  306.             break;
  307.         case 27:
  308.             *quit = 1;
  309.             while ((ch = getKey(100)) != ERR)
  310.                 *quit = (ch == 'x'); 
  311.             break;
  312.         default:
  313.             if ((ch > 0) && (ch <= 255))
  314.                 editSearch(ch);
  315.             else {
  316.                 dataRefresh(_x0, _y0, _curNode);
  317.                 beep();
  318.             }
  319.             break;
  320.         }
  321.     } while (!*quit);
  322.  
  323.     if (*quit<=2)    
  324.         ioTerminate();
  325.     
  326.     if (*quit==2)
  327.         return _curNode;
  328.     else
  329.         return NULL;
  330. }
  331.  
  332. /*************************************************************************/
  333.